# 论坛系统注册页自动化测试
from selenium.webdriver.support import expected_conditions as EC
from time import sleep

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

from ForumAutoTest.Common.Utils import ForumDriver


class ForumRegister:
    url = ""
    driver = ""

    def __init__(self):
        self.url = "http://127.0.0.1:58080/sign-up.html"
        self.driver = ForumDriver.driver
        self.driver.get(self.url)

    # 正常注册成功
    def RegisterSuccess(self):
        self.driver.get(self.url)
        # 定位用户注册确确保在注册页面
        self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > h2")
        # 截图--1
        ForumDriver.getScreeShot()

        # 用户名
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("888888888")
        # 昵称
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").send_keys("monkey1")
        # 设置密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        # 确认密码
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys("123456")
        #同意协议
        self.driver.find_element(By.CSS_SELECTOR, "#policy").click()
        sleep(1)
        # 截图--2
        ForumDriver.getScreeShot()

        #点击截图并截图却确认是否注册成功
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        sleep(1)
        # 截图--3
        ForumDriver.getScreeShot()


    # 无法注册
    def  Registerfail(self):
        #进入注册页
        self.driver.get("http://127.0.0.1:58080/sign-up.html")
        # 截图--1
        ForumDriver.getScreeShot()

        #用户名为空，其它不为空
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("")
        # 昵称
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").send_keys("monkey2")
        # 设置密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        # 确认密码
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys("123456")
        # 同意协议
        self.driver.find_element(By.CSS_SELECTOR, "#policy").click()
        #点击注册
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        case = self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > div:nth-child(2) > div").text
        print(case)
        assert case == "用户名不能为空"
        sleep(1)
        # 截图--2
        ForumDriver.getScreeShot()




        #重新进入注册页
        self.driver.get("http://127.0.0.1:58080/sign-up.html")
        #昵称为空，其它不为空
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("777")
        # 昵称
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").send_keys("")
        # 设置密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        # 确认密码
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys("123456")
        # 同意协议
        self.driver.find_element(By.CSS_SELECTOR, "#policy").click()
        #点击注册
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        case = self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > div:nth-child(3) > div").text
        print(case)
        assert case == "昵称不能为空"
        sleep(1)
        # 截图--3
        ForumDriver.getScreeShot()



        # 重新进入注册页
        self.driver.get("http://127.0.0.1:58080/sign-up.html")
        #密码为空，其它不为空
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("777")
        # 昵称
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").send_keys("monkey2")
        # 设置密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("")
        # 确认密码
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys("123456")
        # 同意协议
        self.driver.find_element(By.CSS_SELECTOR, "#policy").click()
        #点击注册
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        case = self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > div:nth-child(4) > div > div").text
        print(case)
        assert case == "密码不能为空"
        sleep(1)
        # 截图--4
        ForumDriver.getScreeShot()




        # 重新进入注册页
        self.driver.get("http://127.0.0.1:58080/sign-up.html")
        #不确认密码，其它不为空
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("777")
        # 昵称
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").send_keys("monkey2")
        # 设置密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        # 确认密码
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys("")
        # 同意协议
        self.driver.find_element(By.CSS_SELECTOR, "#policy").click()
        #点击注册
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        case = self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > div:nth-child(5) > div > div").text
        print(case)
        assert case == "请检查确认密码"
        sleep(1)
        # 截图--5
        ForumDriver.getScreeShot()




        # 重新进入注册页
        self.driver.get("http://127.0.0.1:58080/sign-up.html")
        #未同意协议，其它不为空
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("777")
        # 昵称
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").send_keys("monkey2")
        # 设置密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        # 确认密码
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys("123456")
        # 点击注册
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        print("未同意协议")
        sleep(1)
        # 截图--6
        ForumDriver.getScreeShot()




        # 重新进入注册页
        self.driver.get("http://127.0.0.1:58080/sign-up.html")
        #输入已经存在的账户
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        # 昵称
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").send_keys("程序猿")
        # 设置密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("1234")
        # 确认密码
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys("1234")
        # 同意协议
        self.driver.find_element(By.CSS_SELECTOR, "#policy").click()
        sleep(10)
        # 截图--7
        ForumDriver.getScreeShot()
        # 点击注册
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        wait = WebDriverWait(self.driver, 2)
        wait.until(
            EC.visibility_of_element_located((By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right > div")))
        warning_message = self.driver.find_element(By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right > div").text
        print(warning_message)
        assert "用户已存在" in warning_message
        sleep(1)
        # 截图--8
        ForumDriver.getScreeShot()

        #点击登录
        self.driver.find_element(By.CSS_SELECTOR, "body > div > div > div.text-center.text-muted.mt-3 > a").click()
        sleep(1)
        # 截图--9
        ForumDriver.getScreeShot()


